package com.visitor.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.PageInfo;
import com.visitor.bean.VisitorRecord;
import com.visitor.service.VisitorRecordService;
import com.visitor.utils.DateUtils;
import com.visitor.web.vm.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(tags="历史预约记录管理")
@Controller
@RequestMapping(value = "visitorRecordManager")
public class VisitorRecordController {
	@Autowired
	private VisitorRecordService smartUserService;

	@ApiOperation(value="按照编号删除预约信息",notes="按照编号删除预约信息")
	@ApiImplicitParam(name="id",value="预约编号",paramType="path",required=true)
	@ResponseBody
	@PostMapping("/delVisitorRecord/{id}")
	public Response<String> delUser(@PathVariable("id")Long id){
		smartUserService.deleteUserById(id);
		return Response.ok("success");
	}

	@GetMapping("/visitorList")
	public ModelAndView visitorList(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
								  @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
									String appointmentTime,
									ModelAndView modelAndView) {
		PageInfo<VisitorRecord> data= smartUserService.getAllByPage(pageNum, pageSize,appointmentTime);
		modelAndView.addObject("page",data);
		modelAndView.setViewName("visitorRecord/visitor-list");
		return modelAndView;
	}

	//到处访客记录
	@GetMapping("/exportExcel")
	public void exportExcel(HttpServletResponse response) {
		try{
			List<List<String>> rows =new ArrayList<>();
			List<String> row1 = CollUtil.newArrayList("访客姓名", "访客手机号", "被访人姓名", "被访人电话", "预约日期", "访问事由");
			rows.add(row1);
			List<VisitorRecord>	list=smartUserService.getAll();
			for(VisitorRecord vr:list){
				rows.add(CollUtil.newArrayList(vr.getVisitorName(),  vr.getPhone(),vr.getUserPhone(),vr.getUserName(),vr.getAppointmentTime(),vr.getReasons()));
			}
			ExcelWriter writer = ExcelUtil.getWriter();
			writer.write(rows);
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setHeader("Content-Disposition","attachment;filename="+ DateUtils.getTime3()+"visitorRecord.xls");
			ServletOutputStream out=response.getOutputStream();
			writer.flush(out);
			writer.close();
			IoUtil.close(out);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	//跳转添加预约页面
	@GetMapping("/visitor-add")
	public ModelAndView visitorAddHtml(ModelAndView modelAndView) {
		modelAndView.setViewName("visitorRecord/visitor-add");
		return modelAndView;
	}

	@ApiOperation(value="添加预约",notes="添加预约")
	@ResponseBody
	@PostMapping("/addVisitorRecord")
	public Response<String> addUser(VisitorRecord visitor){
		return smartUserService.save(visitor);
	}

	//跳转添加预约页面
	@GetMapping("/visitor-edit")
	public ModelAndView visitorEditHtml(Long id,ModelAndView modelAndView) {
		VisitorRecord visitor=smartUserService.findUserById(id);
		modelAndView.addObject("visitor",visitor);
		modelAndView.setViewName("visitorRecord/visitor-edit");
		return modelAndView;
	}

}
